<template>
	<view class="container">
		<view class="catetop">
			<my-search @click="gotoSearch"></my-search>
			<image src="../../static/my-icons/catetitie.jpg" mode="" style="height: 100px;"></image>
		</view>

		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<block v-for="(item, i) in categories" :key="i">
					<view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChanged(i)">
						{{item}}
					</view>
				</block>
			</scroll-view>
			<!-- 右侧的滚动视图区域 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view class="cate-lv2" v-for="(item2, i2) in catelist" :key="i2" @click="gotoDetail(item2)">
					<view class="img">
						<image :src="item2.imageURL" class="left"></image>
					</view>
					<view class="right">

						<view class="right1">
							<text>{{item2.description}}</text>
						</view>

						<view class="right2">
							<text>￥{{item2.price}}</text>
							<view class="icon-wrapper">
								<uni-icons type="plus-filled" size="25" color="#c0d1ff"></uni-icons>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<uni-fab :pattern="pattern" horizontal="right" vertical="bottom" :direction="direction"
			@fabClick="fabClick"></uni-fab>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
				wh: 0,
				//一级分类
				categories: [],
				// 当前选中项的索引，默认让第一项被选中
				active: 0,
				// 二级分类列表
				catelist: [],
				pattern: {
					icon: 'cart',
					selectedColor: '#ffffff',
					buttonColor: '#ffffff',
					iconColor: '#aa0000'
				},
			};
		},
		onLoad() {
			// 获取当前系统的信息
			const sysInfo = uni.getSystemInfoSync();
			// 为 wh 窗口可用高度动态赋值
			this.wh = sysInfo.windowHeight;
			this.allCategories().then(() => {
				// 确保在categories加载完成后调用activeChanged
				this.activeChanged(0);
			});
		},
		onPullDownRefresh() {
			// 显示下拉刷新时的加载动画
			this.activeChanged(0).finally(() => {
				// 停止下拉刷新动画
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			gotoSearch() {
				// 实现跳转到搜索页面的逻辑
				uni.navigateTo({
				  url: '/subpkg/search/search'
				})
			},
			async allCategories() {
				try {
					const res = await uni.$http.get('/allCategories');
					this.categories = res.data.data
					console.log('类别：', res.data.data)
				} catch (error) {
					console.error('获取类别时发生错误:', error);
				}
			},
			// 选中项改变的事件处理函数
			async activeChanged(i) {
				this.active = i;
				console.log(this.categories[i]);
				try {
					const res = await uni.$http.get(`/catelist/${this.categories[i]}`);
					console.log('查询各类别数据：', res);
					this.catelist = res.data.data;
					console.log('类别数据：', this.catelist);
				} catch (error) {
					console.error(error);
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				}
			},
			// 点击跳转到商品详情页面
			gotoDetail(item2) {
				console.log(item2.productId)
				uni.navigateTo({
					url: '/subpkg/product-info/product-info?productId=' + item2.productId
				})
			},
			fabClick() {
				wx.switchTab({
					url: '/pages/cart/cart'
				});
			}
		}

	};
</script>

<style scoped lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ffffff;
					position: relative;

					// 渲染激活项左侧的红色指示边线
					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}

		.cate-lv2 {
			// width: 400px;
			height: 100px;
			display: flex;
			align-items: center;
			background: #ffffff;
			padding-left: 10px;
			// padding-top: 10px;
			border-bottom: 1px solid #ccc;

			.right {
				width: 150px;
				margin-left: 10px;

				.right1 {
					font-size: 12px;
					margin-top: -10px;
				}

				.right2 {
					display: flex;
					align-items: center;
					justify-content: flex-start;
					color: #D81E06;
					font-size: 15px;
					margin: 12px 0 5px 0;

					.icon-wrapper {
						margin-left: auto; // 将图标推到右边
						padding-right: 5px;
					}
				}
			}

			.img {
				width: 80px;

				.left {
					width: 80px;
					height: 80px;
				}
			}

		}
	}
</style>